package com.adcc.loadsheet.monitor.util;

import com.adcc.aoc.devLib.common.log.Log;
import com.google.common.base.Strings;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

    /**
     * 获取IP地址
     *
     * @param url
     * @return
     */
    public static String getIP(String url) {
        String ip = Strings.emptyToNull("");
        String regexString = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
        Pattern p = Pattern.compile(regexString);
        Matcher m = p.matcher(url);
        boolean result = m.find();
        if (result) {
            ip = m.group();
        }
        return ip;
    }

    /**
     * StringBuilder 转成字符串，并截取掉最后一位“,”
     * @param stringBuilder
     * @return
     */
    public static String stringBuilderToString(StringBuilder stringBuilder) {
        String str = "";
       try {
           str = stringBuilder.toString();
           if (str.length() > 0) {
               str = str.substring(0, str.length() - 1);
           }
           return str;
       }catch (Exception e){
           Log.error(StringUtil.class, "stringBuilderToString() error", e);
           return "";
       }
    }
}
